package com.etlmaster.executor.steps.file;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.utils.LogWriter;

public class FileTransferStepMeta extends StepMeta {
	
	
	private String sourceVfsPath;//源vfs路径
	
	private String targetVfsPath;//目标vfs路径
	
	private String sourceCharset="UTF-8";//源字符集
	
	private String targetCharset="UTF-8";//源字符集
	
	private boolean isAppend;//是否追加写入
	
	private boolean isRowRead;//是否行读
	
	private String startRowNum="1";
	
	private String rowProcessScript;//行处理方法
	
	private String splitType;
	
	private String rowNum;
	
	private String fileSize;
	
	private String fileMergeSize;
	private CompiledScript compiledScript;
	private Bindings bindings;
	
	private boolean deCompress;//是否自动解压
	
	private boolean keepBack;//是否保留压缩文件

	public boolean isDeCompress() {
		return deCompress;
	}

	public void setDeCompress(boolean deCompress) {
		this.deCompress = deCompress;
	}

	public boolean isKeepBack() {
		return keepBack;
	}

	public void setKeepBack(boolean keepBack) {
		this.keepBack = keepBack;
	}
	
	public FileTransferStepMeta(String id, String name,String step_code, String session_id, String paramStr) {
		super(id, name,step_code, session_id, paramStr);
	
		parseParam(paramStr);
	}
	
	public String getSourceVfsPath() {
		return variableSubstitution(sourceVfsPath);
	}

	public void setSourceVfsPath(String sourceVfsPath) {
		this.sourceVfsPath = sourceVfsPath;
	}

	public String getTargetVfsPath() {
		return variableSubstitution(targetVfsPath);
	}

	public void setTargetVfsPath(String targetVfsPath) {
		this.targetVfsPath = targetVfsPath;
	}

	public String getSourceCharset() {
		return variableSubstitution(sourceCharset);
	}

	public void setSourceCharset(String sourceCharset) {
		this.sourceCharset = sourceCharset;
	}

	public String getTargetCharset() {
		return targetCharset;
	}

	public void setTargetCharset(String targetCharset) {
		this.targetCharset = targetCharset;
	}

	public boolean isAppend() {
		return isAppend;
	}

	public void setAppend(boolean isAppend) {
		this.isAppend = isAppend;
	}
	
	public boolean needChangeChatset(){
		return !getSourceCharset().equalsIgnoreCase(getTargetCharset());
	}

	public boolean isRowRead() {
		return isRowRead;
	}

	public void setRowRead(boolean isRowRead) {
		this.isRowRead = isRowRead;
	}

	public String getStartRowNum() {
		return variableSubstitution(startRowNum);
	}

	public void setStartRowNum(String startRowNum) {
		this.startRowNum = startRowNum;
	}

	public String getRowProcessScript() {
		return variableSubstitution(rowProcessScript);
	}

	public void setRowProcessScript(String rowProcessScript) {
		this.rowProcessScript = rowProcessScript;
	}

	public String rowProcessor(String row){
		if(rowProcessScript!=null && rowProcessScript.length()>0){
			try {
				if(compiledScript==null){
					ScriptEngineManager sem = new ScriptEngineManager();
				    ScriptEngine engine = null;
				    try{
				    	engine = sem.getEngineByName("nashorn");
				    	if(engine==null){
				    		LogWriter.addLog("DEBUG","创建nashorn引擎失败，未使用Jdk 8，使用默认Rhino引擎");
					    	engine = sem.getEngineByName("JavaScript");
				    	}
				    }catch(Exception e){
				    	LogWriter.addLog("DEBUG","创建nashorn引擎失败，不是使用Jdk 8使用默认Rhino引擎，{}",e.getMessage());
				    	engine = sem.getEngineByName("JavaScript");
				    }
				    Compilable compilable = (Compilable) engine;
				    StringBuilder script = new StringBuilder("(function(){ ");
					script.append(variableSubstitution(rowProcessScript)).append("})()");
					LogWriter.addLog("INFO","行处理脚本为：{}",script.toString());
					compiledScript = compilable.compile(script.toString());
					bindings = engine.createBindings();
				}
				bindings.put("row", row);
				Object obj = compiledScript.eval(bindings);
				return obj!=null ? obj.toString() : "";
			} catch (ScriptException e) {
				e.printStackTrace();
			}
		}
		return row;
//		return row.replaceAll("\r", "");
	}

	@Override
	protected void loadParamsFromConfigTable() {
		
	}

	public String getSplitType() {
		return variableSubstitution(splitType);
	}

	public void setSplitType(String splitType) {
		this.splitType = splitType;
	}

	public String getRowNum() {
		return variableSubstitution(rowNum);
	}

	public void setRowNum(String rowNum) {
		this.rowNum = rowNum;
	}

	public String getFileSize() {
		return variableSubstitution(fileSize);
	}

	public void setFileSize(String fileSize) {
		this.fileSize = fileSize;
	}

	public String getFileMergeSize() {
		return variableSubstitution(fileMergeSize);
	}

	public void setFileMergeSize(String fileMergeSize) {
		this.fileMergeSize = fileMergeSize;
	}

}
